import {
	defineStore
} from 'pinia'
import {
	getHomeData,
	getJuanpiHomeData
} from '../service/home'
import {
	objectToarray
} from '../utils/homeformat'
export const types=['pop','new','sell']
// function getDefaultGoodsListData(){
// 	let goodsListorign={}
// 	types.forEach((type)=>{
// 		goodsListorign[type]={page:0,list:[]}
// 	})
// 	return goodsListorign
// }

export const useHomeStore = defineStore('home', {
	state: () => {
		return {
			banners: [],
			recommend: [],
			// goodsList: getDefaultGoodsListData()
			goodsList: {
				'pop': {
					page: 0,
					list: []
				},
				'new': {
					page: 0,
					list: []
				},
				'sell': {
					page: 0,
					list: []
				},
			
			},
			currentType: 'pop',
		}
	},
	actions: {
       setCurrentType(type) {
			this.currentType = type
		},
		// 异步发送网络请求 - 轮播图-推荐列表数据
		async fechHomeMutiData() {
			const res = await getHomeData()
			// console.log('fech',res);

			this.banners = objectToarray(res.data.recommend)
			this.recommend = res.data.populars
		},
		async fechHomeGoodsData(type, page) {
			// console.log(type,page);
			const res = await getJuanpiHomeData(type, page)
			console.log(res.data.goods);
			this.goodsList[type].page=page
			this.goodsList[type].list.push(...res.data.goods)
		}

	}
})
